const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
  const User = sequelize.define("User", {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      allowNull: false,
      primaryKey: true,
    },

    username: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
      validate: {
        len: [6, 18],
        isAlphanumeric: true, // 用户名只能包含字母和数字
      },
      comment: "用户名",
    },

    password: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {},
      comment: "密码",
    },

    role: {
      type: DataTypes.STRING,
      defaultValue: 1,
      comment: "角色权限 0-管理员 1-用户",
    },
  });

  User.associate = (models) => {
    User.hasMany(models.Post, { foreignKey: "userId" });
    User.hasMany(models.Comment, { foreignKey: "userId" });
  };

  return User;
};
